package com.wuliupai.delivery.wuliupaidelivery.activity;

import android.text.TextUtils;
import android.widget.EditText;

import com.wuliupai.delivery.wuliupaidelivery.App;
import com.wuliupai.delivery.wuliupaidelivery.R;
import com.wuliupai.delivery.wuliupaidelivery.bean.BaseBean;
import com.wuliupai.delivery.wuliupaidelivery.bean.LoginBean;
import com.wuliupai.delivery.wuliupaidelivery.constants.Value;
import com.wuliupai.delivery.wuliupaidelivery.net.NetResultOperate;
import com.wuliupai.delivery.wuliupaidelivery.ui.MyToolbar;
import com.wuliupai.delivery.wuliupaidelivery.utils.JsonUtils;
import com.wuliupai.delivery.wuliupaidelivery.utils.LogUtils;
import com.wuliupai.delivery.wuliupaidelivery.utils.MD5;
import com.wuliupai.delivery.wuliupaidelivery.utils.StringUtils;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

/**
 * Descriptions：登录
 * <p>
 * Author：ChenME
 * Date：11/11/2016
 * Email：ibelieve1210@163.com
 */
@EActivity(R.layout.activity_login)
public class LoginActivity extends AppBaseActivity {

    @ViewById(R.id.et_account)
    EditText et_account;//账号
    @ViewById(R.id.et_password)
    EditText et_password;//密码

    @AfterViews
    void onPageStart() {
        MyToolbar toolbar = (MyToolbar) findViewById(R.id.toolbar);
        toolbar.setTitleInCenter(true);
        toolbar.setTitle(getString(R.string.string_login_title));
        BaseBean baseBean = new BaseBean();
        if (!StringUtils.isEmpty(baseBean.getLoginName())) {
            et_account.setText(baseBean.getLoginName());
        }
    }

    /**
     * 登录
     */
    @Click(R.id.btn_loginBtn)
    void login() {

        String account = et_account.getText().toString().trim();
        String password = et_password.getText().toString().trim();

        if (TextUtils.isEmpty(account)) {
            App.toast(R.string.string_login_accountHint);
            return;
        }

        if (TextUtils.isEmpty(password)) {
            App.toast(R.string.string_login_passwordHint);
            return;
        }

        password = MD5.GetMD5Code(password);
        LoginBean loginBean = new LoginBean(account, password, "1.0");
        String loginJson = JsonUtils.createJsonString(loginBean);
        try {
            String aes = JsonUtils.aes(loginJson);
            callNet(this, "loginbypassword", aes, new NetResultOperate() {
                @Override
                public void onSuccess(String content) {
                    LoginBean parseLogin = (LoginBean) JsonUtils.parseJsonString(content, LoginBean.class);
                    App.pushAgent.removeAlias(BaseBean.UserType + parseLogin.getUserId(), Value.UMengPush_Alias,
                            (b, s) -> LogUtils.log("umeng友盟---" + b + "," + s));
                    App.pushAgent.addAlias(BaseBean.UserType + parseLogin.getUserId(), Value.UMengPush_Alias,
                            (b, s) -> LogUtils.log("umeng友盟---" + b + "---" + s + "---"+ parseLogin.getUserId()));
                    BaseBean baseBean = new BaseBean();
                    baseBean.setLoginName(account);
                    baseBean.setToken(parseLogin.getToken());
                    baseBean.setUserId(parseLogin.getUserId());
                    baseBean.save();
                    TaskCurrentListActivity_.intent(LoginActivity.this).start();
                    finish();
                    animUtils.leftInAnimation(LoginActivity.this);
                }

                @Override
                public void onDataEmpty(String content) {
                }

                @Override
                public void onDataEnd(String content) {
                }
            });

        } catch (Exception e) {
            LogUtils.log("Json串加密出现异常~~" + e.toString());
        }
    }

    /**
     * 忘记密码
     */
    @Click(R.id.tv_forgetPasswordBtn)
    void forgetPassword() {
    }
}